package cmd

import (
	"fmt"
	"gitee.com/getcharzp/light-ftp/define"
	"gitee.com/getcharzp/light-ftp/internal/service/config"
	"github.com/spf13/cobra"
	"os"
)

type configCommand struct {
	cmd *cobra.Command
}

func newConfigCommand() *cobra.Command {
	c := new(configCommand)
	c.cmd = &cobra.Command{
		Use:   "config",
		Short: "config light ftp",
		Run: func(cmd *cobra.Command, args []string) {
			if err := cmd.Help(); err != nil {
				fmt.Println("light ftp help error.", err)
				os.Exit(1)
			}
		},
	}

	c.create()
	c.edit()
	c.list()
	c.delete()

	return c.cmd
}

func (c *configCommand) create() {
	var cfg define.SiteConfig
	cmd := &cobra.Command{
		Use:   "create",
		Short: "create light ftp config",
		Run: func(cmd *cobra.Command, args []string) {
			config.Create(&cfg)
		},
	}
	cmd.Flags().StringVarP(&cfg.Name, "name", "n", "", "ftp config name")
	cmd.Flags().StringVarP(&cfg.Type, "type", "", "", "ftp config type")
	cmd.Flags().StringVarP(&cfg.Host, "host", "", "", "ftp config host")
	cmd.Flags().StringVarP(&cfg.Port, "port", "", "", "ftp config port")
	cmd.Flags().StringVarP(&cfg.Account, "account", "", "", "ftp config account")
	cmd.Flags().StringVarP(&cfg.Password, "password", "", "", "ftp config password")
	c.cmd.AddCommand(cmd)
}

func (c *configCommand) edit() {
	var cfg define.SiteConfig
	cmd := &cobra.Command{
		Use:   "edit",
		Short: "edit light ftp config",
		Run: func(cmd *cobra.Command, args []string) {
			config.Edit(cfg.Name)
		},
	}
	cmd.Flags().StringVarP(&cfg.Name, "name", "n", "", "ftp config name")
	c.cmd.AddCommand(cmd)
}

func (c *configCommand) delete() {
	var cfg define.SiteConfig
	cmd := &cobra.Command{
		Use:   "delete",
		Short: "delete light ftp config",
		Run: func(cmd *cobra.Command, args []string) {
			config.Delete(cfg.Name)
		},
	}
	cmd.Flags().StringVarP(&cfg.Name, "name", "n", "", "ftp config name")
	c.cmd.AddCommand(cmd)
}

func (c *configCommand) list() {
	cmd := &cobra.Command{
		Use:   "list",
		Short: "list light ftp config",
		Run: func(cmd *cobra.Command, args []string) {
			config.List()
		},
	}
	c.cmd.AddCommand(cmd)
}
